package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {
    @Autowired
    private ArticleFeign articleFeign;
    @Resource
    private ApUserFollowMapper apUserFollowMapper;
    @Resource
    private ApUserFanMapper apUserFanMapper;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //判断参数
        if (dto==null||dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //声明follow_id
        Integer followId=null;
        ApAuthor apAuthor = articleFeign.findByAuthorId(dto.getAuthorId());
        if (apAuthor!=null){
            followId=apAuthor.getId();
        }
        //获取登录的用户的userId
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //判定followId
        if (followId!=null){
            if (dto.getOperation()==0){//关注
                followAuthor(user,followId,apAuthor);
            }else {//取消关注
                cancelFollowAuthor(user,followId);
            }
        }
        return ResponseResult.okResult(200,"执行成功");
    }

    //关注
    public ResponseResult followAuthor(ApUser user,Integer followId,ApAuthor apAuthor){
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, followId));
        if (apUserFollow==null){
            ApUserFollow apUserFollow1 = new ApUserFollow();
            apUserFollow1.setCreatedTime(new Date());
            apUserFollow1.setFollowId(followId);
            apUserFollow1.setFollowName(apAuthor.getName());
            apUserFollow1.setIsNotice(true);
            apUserFollow1.setLevel(1);
            apUserFollow1.setUserId(user.getId());
            apUserFollowMapper.insert(apUserFollow1);
            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, user.getId()));
            if (apUserFan==null){
                ApUserFan apUserFan1 = new ApUserFan();
                apUserFan1.setCreatedTime(new Date());
                apUserFan1.setFansId(user.getId().longValue());
                apUserFan1.setFansName(apAuthor.getName());
                apUserFan1.setIsDisplay(true);
                apUserFan1.setIsShieldLetter(true);
                apUserFan1.setLevel(1);
                apUserFan1.setUserId(apAuthor.getUserId());
                apUserFanMapper.insert(apUserFan1);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
    }

    //取消关注
    public ResponseResult cancelFollowAuthor(ApUser user,Integer followId){
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, followId));
        if (apUserFollow!=null){
            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId,followId));
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, user.getId()));
        if (apUserFan!=null){
            apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,user.getId()));
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



}
